home *** CD-ROM | disk | FTP | other *** search
- Path: news.atw.fullfeed.com!usenet
- From: lam@adci.com (Lori A. MacVittie)
- Newsgroups: comp.lang.c++
- Subject: Re: Newbie question re: struct pointers & member access
- Date: 8 Mar 1996 14:56:58 GMT
- Organization: ADCI
- Message-ID: <4hphrr$4s0@ray.atw.fullfeed.com>
- References: <4hn0v2$7q5@aahz.magic.mb.ca>
- NNTP-Posting-Host: 204.145.195.173
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.6
-
- In article <4hn0v2$7q5@aahz.magic.mb.ca>, jranta@astral.magic.ca@
- says...
-
- >void enter(int t)
- >{
- > car *ptr = new car[t];
- > for(int a=0;a<t;a++,ptr++)
- > {
- > cin.get(); //to clear input queue(alt. between strings &
- numbers).
- > cout<<"Car #"<<a+1;
- > cout<<"\nPlease enter make: ";
- > cin.getline(ptr->make,ArSize);
- > cout<<"\nEnter year: ";
- > cin>>ptr->year;
- > }
- >return;
- >}
-
- You need to grab ahold of the pointer before you increment it
- with another variable.
-
- In addition to this, you need to decide what it is you have..
-
- The code you show [ car *ptr = new char[t] ] is allocating a
- pointer to an array.If you increment the pointer, you should
- dereference it before attempting to assign values to the data
- members. Think of it like you would a char* ..
-
- char* ptr = new char[t];
- char* hold = ptr;
-
- for (int a=0; a<t; a++, ptr++ )
- {
- if ( *ptr == 'a' )
- // do something
- }
-
- // now use the hold variable to iterate the array again
- for (a=0; a < t; a++)
- cout << "letter " << a << " = " << hold[a] << '\n';
-
- Although an easier way would be simply to access your new array
- of car by index:
-
- cin.getline( ptr[a].make, ArSize );
-
- If you actually want a pointer to an array of pointers, you need
- to do it a bit differently:
-
- car **ptr = new car*[t];
-
-
- Lori.
-
-